//
//  TempHomeController.swift
//  JianTingSwift
//
//  Created by zhibao on 2024/11/21.
//

import UIKit

class TempHomeController: SuperController,UITableViewDelegate,UITableViewDataSource,DataChangeDelegate {
    fileprivate var showTableView = MYTableView(frame: CGRectMake(0, navigationFullHeight(), SCREEN_WIDTH, SCREEN_HEIGHT - navigationFullHeight() - tabBarFullHeight()), style: .plain)
    
    fileprivate var dataArray = [[String:String]]()
    
    var gongnengArray = [
        [
            "title":"全新手机",
            "suoTitle":"全新",
            "type":NEW
        ],
        [
            "title":"二手手机",
            "suoTitle":"二手",
            "type":OLD
        ],
        [
            "title":"平板电脑",
            "suoTitle":"平板",
            "type":PINGBAN
        ],
        [
            "title":"数码相机",
            "suoTitle":"数码",
            "type":SHUMA
        ],
    ]
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if UserDefaults.standard.value(forKey: "privacy1") == nil {
            Helper.privacyAgreementShow()
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.getPayInfo()
        
        if MYGlobal.share().wechatFlag != 1 {
            gongnengArray = [
                [
                    "title":"全新手机",
                    "suoTitle":"全新",
                    "type":NEW
                ],
                [
                    "title":"二手手机",
                    "suoTitle":"二手",
                    "type":OLD
                ],
                [
                    "title":"平板电脑",
                    "suoTitle":"平板",
                    "type":PINGBAN
                ],
                [
                    "title":"数码相机",
                    "suoTitle":"数码",
                    "type":SHUMA
                ],
            ]
        }
        self.otherInitView()
        self.initTableview()
        self.gk_statusBarStyle = .lightContent;
        self.hideNavLine()
    }
    
    fileprivate func getPayInfo(){
        if IAPShare.sharedHelper().iap == nil {
            let dataSet = NSSet(array: [
                "com.yuliang.jianting.zhou",
                "com.yuliang.jianting.yue",
                "com.yuliang.jianting.ji",
                "com.yuliang.jianting.nian"
            ])
            IAPShare.sharedHelper().iap = IAPHelper(productIdentifiers: dataSet as? Set<AnyHashable>)
        }
        IAPShare.sharedHelper().iap.requestProducts { request, response in
            DLog(message: "response1==\(String(describing: response?.products))")
        }
        IAPShare.sharedHelper().iap.production = false
    }

    
    fileprivate func otherInitView(){
        let leftNavImgView = UIImageView(image: UIImage(named: "h_nav_left"))
        self.gk_navigationBar.addSubview(leftNavImgView)
        leftNavImgView.snp.makeConstraints { make in
            make.left.equalTo(pixw(p:5))
            make.bottom.equalTo(pixw(p: -4))
            make.width.equalTo(pixw(p: 120))
            make.height.equalTo(pixw(p: 40))
        }
        self.gk_navigationBar.backgroundColor = MYColor.color(withHexString: "f94123")
    }
    fileprivate func initTableview(){
        self.createHeadView()
        showTableView.delegate = self
        showTableView.dataSource = self
        showTableView.dataChangeDelegate = self
        showTableView.separatorStyle = .none
        showTableView.register(ProjectCell.self, forCellReuseIdentifier: "ProjectCell")
        self.view.addSubview(showTableView)
        showTableView.noDataType = .TableViewNoDataType_FILE
        showTableView.mj_footer?.endRefreshingWithNoMoreData()
        self.getData()
        
    }
    
    fileprivate func createHeadView(){
        let btnWidth = (SCREEN_WIDTH - 5 * pixw(p: 24)) / 4
        let headView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: pixw(p: 343) + 79 + btnWidth))
        self.showTableView.tableHeaderView = headView
        
        let topView = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: pixw(p: 100)))
        topView.backgroundColor = MYColor.color(withHexString: "f94123")
        headView.addSubview(topView)
        
        let imgView = UIImageView(image: UIImage(named: "h_top_first"))
        imgView.frame = CGRect(x: pixw(p: 16), y: pixw(p: 20), width: SCREEN_WIDTH - pixw(p: 32), height: pixw(p: 130))
        headView.addSubview(imgView)
        
        
        var top = imgView.bottom
        for i in 0 ..< gongnengArray.count {
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x: pixw(p: 24) + (btnWidth + pixw(p: 24)) * CGFloat(i), y: top + pixw(p: 20), width: btnWidth, height: btnWidth + pixw(p: 24))
            btn.tag = 1000 + i
            btn.addTarget(self, action: #selector(fenleiClick(sender: )), for: .touchUpInside)
            headView.addSubview(btn)
            if i == gongnengArray.count - 1 {
                top = btn.bottom
            }
            
            let suoLabel = UILabel()
            suoLabel.backgroundColor = MYColor.color(withHexString: "dbdbdb")
            suoLabel.layer.cornerRadius = btnWidth / 2
            suoLabel.layer.masksToBounds = true
            suoLabel.font = MY_SCALE_BoldFont(20)
            suoLabel.text = gongnengArray[i]["suoTitle"]
            suoLabel.textColor = MYRed
            suoLabel.textAlignment = .center
            btn.addSubview(suoLabel)
            suoLabel.snp.makeConstraints { make in
                make.left.right.top.equalTo(btn)
                make.height.equalTo(btnWidth)
            }
            
            let titleLabel = UILabel()
            titleLabel.font = MY_SCALE_MediumFONT(13)
            titleLabel.text = gongnengArray[i]["title"]
            titleLabel.textColor = MYBlock
            titleLabel.textAlignment = .center
            btn.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { make in
                make.left.bottom.right.equalTo(btn)
            }
        }
        
        //通告
        let tonggaoView = UIView(frame: CGRect(x: pixw(p: 16), y: top + pixw(p: 28), width: SCREEN_WIDTH - pixw(p: 32), height: pixw(p:79)))
        tonggaoView.backgroundColor = MYColor.color(withHexString: "f1f8ff")
        headView.addSubview(tonggaoView)
        
        if tonggaoView != nil {
            let tImgView = UIImageView(image: UIImage(named: "h_tonggao"))
            tonggaoView.addSubview(tImgView)
            tImgView.snp.makeConstraints { make in
                make.left.equalTo(pixw(p: 13))
                make.centerY.equalTo(tonggaoView)
                make.width.equalTo(pixw(p: 28))
                make.height.equalTo(pixw(p: 14))
            }
            
            let content1Label = UILabel()
            content1Label.font = MY_SCALE_RegularFONT(12)
            content1Label.text = "会员专享一：所有商品免费包邮。"
            content1Label.textColor = MYColor.color(withHexString: "999999")
            tonggaoView.addSubview(content1Label)
            content1Label.snp.makeConstraints { make in
                make.left.equalTo(tImgView.snp.right).offset(pixw(p: 13))
                make.right.equalTo(pixw(p: -15))
                make.top.equalTo(10)
            }
            
            let content2Label = UILabel()
            content2Label.font = MY_SCALE_RegularFONT(12)
            content2Label.text = "会员专享二：会员等级不同，折扣不同。"
            content2Label.textColor = MYColor.color(withHexString: "999999")
            tonggaoView.addSubview(content2Label)
            content2Label.snp.makeConstraints { make in
                make.left.right.equalTo(content1Label)
                make.top.equalTo(content1Label.snp.bottom).offset(5)
            }
            
            let content3Label = UILabel()
            content3Label.font = MY_SCALE_RegularFONT(12)
            content3Label.text = "3分钟前用户133****3333成功租赁小米5手机一…"
            content3Label.textColor = MYColor.color(withHexString: "999999")
            tonggaoView.addSubview(content3Label)
            content3Label.snp.makeConstraints { make in
                make.left.right.equalTo(content1Label)
                make.top.equalTo(content2Label.snp.bottom).offset(5)
            }
        }
        
        //222
        let bannerImgView = UIImageView(frame: CGRect(x: 0, y: tonggaoView.bottom + pixw(p: 16), width: SCREEN_WIDTH, height: pixw(p: 105)))
        headView.addSubview(bannerImgView)
        bannerImgView.image = UIImage(named: "h_banner")
    }
    
    @objc func fenleiClick(sender:UIButton) {
        let controller = FenLeiController()
        controller.fenleiDic = gongnengArray[sender.tag - 1000]
        self.navigationController?.pushViewController(controller, animated: true)
    }
    
    func getData(){
        QMUITips.showLoading(in: self.view)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            QMUITips.hideAllTips()
            self.dataArray = shebeiDic[HOME] ?? []
            self.showTableView.reloadData()
        }
        
    }

}
extension TempHomeController {
    func refreshData(withTabelViewTag tableViewTag: Int) {
        self.getData()
    }
    
    func loadMoreData(withTabelViewTag tableViewTag: Int) {
        self.getData()
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let dic = dataArray[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProjectCell") as! ProjectCell
        cell.updata(dic: dic)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dic = dataArray[indexPath.row]
        let controller = ProjectDetailController()
        controller.dic = dic
        self.navigationController?.pushViewController(controller, animated: true)
    }
}
